/*
 * 工作参数复制
 * 舞者无罪
 * 2014.12.01
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhedit.h"
#include "file_copy.h"
#include "com.h"
#include "dlg_ask.h"
#include "utf8_gb2312.h"
#include "info.h"
#include "run.h"


/*
 * show file list
 */
static void ShowFileList(HWND hWnd)
{
    FILE_COPY *p = (FILE_COPY*)GetWindowAdditionalData(hWnd);
    WND_FILE *pwnd = &p->wnd_file[p->step];
    if(pwnd)
    {
        int size = __min(pwnd->list_size, FILE_LIST_NUM_BY_FILE_BMP);
        int i = 0;
        FILE_INFO *p = pwnd->file_list;
        char name[MH_FILE_NAME_MAX_LEN] = {0};
        for(i = 0; i < size; ++i)
        {
            HWND hWndEdit = GetDlgItem(hWnd, IDC_EDIT1 + i);
            const char eg[] = "0001 123456789ABC 1001024 14-12-18 20:27:30";
            char sc[sizeof(eg)] = {0};
            int index = pwnd->over_top + i;
            sprintf(sc, "%4u ", index + 1);
            UTF_8ToGB2312(name, p[index].file_name, strlen(p[index].file_name) + 1);
            Truncate(&sc[5], name, 13);
            sprintf(&sc[18], "%7u ", p[index].size);
            if(strlen(sc) > 26)
            {
                sprintf(&sc[18], "%6uK ", p[index].size / 1024 + 1);
            }
            struct tm *tm_now;
            tm_now = localtime(&p[index].modify_time);
            sprintf(&sc[26], "%02u-%02u-%02u %02u:%02u:%02u",
                tm_now->tm_year % 100, tm_now->tm_mon + 1, tm_now->tm_mday,
                tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
            EnableWindow(hWndEdit, TRUE);
            SetWindowCaption(hWndEdit, sc);
            InvalidateRect(hWndEdit, NULL, TRUE);
        }
        for(; i < FILE_LIST_NUM_BY_FILE_BMP; ++i)
        {
            HWND hWndEdit = GetDlgItem(hWnd, IDC_EDIT1 + i);
            EnableWindow(hWndEdit, FALSE);
            SetWindowCaption(hWndEdit, "");
            InvalidateRect(hWndEdit, NULL, TRUE);
        }
    }
}
/*
 *
 */
static void CreateFileCopyInterface(HWND hWnd)
{
    int top = 52;
    int left = 11;
    int i = 0;
    for(i = 0; i < FILE_LIST_NUM_BY_FILE_BMP; ++i)
    {
        CreateWindow(MH_EDIT_NAME,
            "",
            WS_CHILD | WS_VISIBLE,
            IDC_EDIT1 + i,
            left,
            top + i * 20,
            420,
            20,
            hWnd, MH_EDIT_TEXT_WHITE | MH_EDIT_ACTIVE_DISABLE);
    }
    int size = GetFileNum(KNIT_FILE_DIR);
    FILE_COPY *pc = (FILE_COPY *)GetWindowAdditionalData(hWnd);
    WND_FILE *pwnd = NULL;
    if(pc)
    {
        pc->wnd_file[0].sort_file_type = GetRunFileSortType();
        pc->wnd_file[1].sort_file_type = pc->wnd_file[0].sort_file_type;
        pwnd = &pc->wnd_file[0];
    }
    if(size && pwnd)
    {
        FILE_INFO *p = calloc(size, sizeof(FILE_INFO));
        char *file_list = calloc(size, sizeof(p->file_name));
        char *suffix_list = calloc(size, sizeof(p->file_suffix));
        GetFileName(KNIT_FILE_DIR, (char (*)[sizeof(p->file_name)])file_list,
                (char(*)[sizeof(p->file_suffix)])suffix_list, size);
        GetFileInfoList(p, (char (*)[sizeof(p->file_name)])file_list,
                (char(*)[sizeof(p->file_suffix)])suffix_list, size, KNIT_FILE_DIR);
        free(file_list);
        free(suffix_list);
        pwnd->file_list = p;
        pwnd->list_size = size;
        if(pwnd->sort_file_type)
        {
            SortFileList(pwnd->file_list, pwnd->list_size, pwnd->sort_file_type);
        }
        ShowFileList(hWnd);
        pc->wnd_file[1].list_size = size;
        pc->wnd_file[1].file_list = calloc(size, sizeof(FILE_INFO));
        memcpy(pc->wnd_file[1].file_list, p, size * sizeof(FILE_INFO));
    }
}
/*
 * select knit file
 */
static int CopyFile(HWND hWnd, const char *from, const char *to)
{
    int ret = 1;
    HWND hDlg = CreateAskDialog(hWnd);
    char sc[MH_FILE_NAME_MAX_LEN * 2 + 128] = {0};
    char name1[MH_FILE_NAME_MAX_LEN] = {0};
    char name2[MH_FILE_NAME_MAX_LEN] = {0};
    UTF_8ToGB2312(name1, from, strlen(from) + 1);
    UTF_8ToGB2312(name2, to, strlen(to) + 1);
    sprintf(sc, MW_ASK_COPY_FILE_CAPTION, name1, name2);
    SetAskInfo(hDlg, sc);
    char *from_name = NULL;
    char *to_name = NULL;
    while(AskDoModal(hDlg))
    {
        int from_len = strlen(from);
        int to_len = strlen(to);
        from_name = calloc(1, sizeof(KNIT_FILE_DIR) + from_len + sizeof(".stp"));
        to_name = calloc(1, sizeof(KNIT_FILE_DIR) + to_len + sizeof(".stp"));

        strcat(from_name, KNIT_FILE_DIR);
        strcat(from_name, from);
        from_len = strlen(from_name);
        strcat(from_name, ".stp");

        strcat(to_name, KNIT_FILE_DIR);
        strcat(to_name, to);
        to_len = strlen(to_name);
        strcat(to_name, ".stp");

        ret = CopyDumuFile(from_name, to_name);
        if(ret)
        {
            break;
        }

        from_name[from_len] = 0;
        strcat(from_name, ".spd");

        to_name[to_len] = 0;
        strcat(to_name, ".spd");

        ret = CopySpeedFile(from_name, to_name);

        from_name[from_len] = 0;
        strcat(from_name, STOP_FILE_SUFFIX);

        to_name[to_len] = 0;
        strcat(to_name, STOP_FILE_SUFFIX);

        CopyStopFile(from_name, to_name);

        break;
    }
    FREE(from_name);
    FREE(to_name)
    return ret;
}
/*
 *
 */
static void OnPageUp(HWND hWnd)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(data.pfile_copy)
    {
        int step = data.pfile_copy->step;
        if(data.pfile_copy->wnd_file[step].over_top < FILE_LIST_NUM_BY_FILE_BMP)
        {
            data.pfile_copy->wnd_file[step].over_top = 0;
        }
        else
        {
            data.pfile_copy->wnd_file[step].over_top -= FILE_LIST_NUM_BY_FILE_BMP;
        }
        ShowFileList(hWnd);
    }
}
/*
 *
 */
static void OnPageDown(HWND hWnd)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(data.pfile_copy)
    {
        int step = data.pfile_copy->step;
        data.pfile_copy->wnd_file[step].over_top += FILE_LIST_NUM_BY_FILE_BMP;
        if(data.pfile_copy->wnd_file[step].over_top + FILE_LIST_NUM_BY_FILE_BMP > data.pfile_copy->wnd_file[step].list_size)
        {
            if(data.pfile_copy->wnd_file[step].list_size > FILE_LIST_NUM_BY_FILE_BMP)
            {
                data.pfile_copy->wnd_file[step].over_top = data.pfile_copy->wnd_file[step].list_size - FILE_LIST_NUM_BY_FILE_BMP;
            }
            else
            {
                data.pfile_copy->wnd_file[step].over_top = 0;
            }
        }
        ShowFileList(hWnd);
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, ushort nID)
{
    FILE_COPY *p = (FILE_COPY *)GetWindowAdditionalData(hWnd);
    switch(nID)
    {
    case ID_ESC:
        if(p)
        {
            if(p->step == FILE_COPY_STEP_SEL_TO)
            {
                p->step = FILE_COPY_STEP_SEL_FROM;
                SetWindowCaption(hWnd, MW_FILE_SEL1_CAPTION);
                ShowFileList(hWnd);
                InvalidateRect(hWnd, NULL, TRUE);
                break;
            }
        }
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
        break;
    case (IDC_EDIT1)...(IDC_EDIT1 + FILE_LIST_NUM_BY_FILE_BMP - 1):
        if(p)
        {
            p->wnd_file[p->step].active = nID - IDC_EDIT1;
            int index = p->wnd_file[p->step].over_top + p->wnd_file[p->step].active;
            if(index < p->wnd_file[p->step].list_size)
            {
                p->name[p->step][0] = 0;
                strcat(p->name[p->step], p->wnd_file[p->step].file_list[index].file_name);
            }
            if(p->step == FILE_COPY_STEP_SEL_TO)
            {
                int ret = CopyFile(hWnd, p->name[0], p->name[1]);
                if(ret < 0)
                {
                    AfxInfoBox(hWnd, NULL, GetString(475));
                }
                else
                {
                    p->step = FILE_COPY_STEP_SEL_FROM;
                    if(!strcmp(GetKnitFileName(g_knit_data), p->name[1]))
                    {
                        char name[MH_FILE_NAME_MAX_LEN] = {0};
                        strcat(name, KNIT_FILE_DIR);
                        strcat(name, GetKnitFileName(g_knit_data));
                        int len = strlen(name);
                        strcat(name, SPEED_FILE_SUFFIX);
                        ret = ReadSpeedFile(name, g_knit_data);
                        if(ret)
                        {
                            //
                        }
                        else
                        {
                            SendSpeedParam(g_knit_data);
                        }
                        name[len] = 0;
                        strcat(name, DUMU_FILE_SUFFIX);
                        ret = ReadDumuFile(name, g_knit_data);
                        if(ret)
                        {
                            //
                        }
                        else
                        {
                            SendDumuParam(g_knit_data);
                        }

                        name[len] = 0;
                        strcat(name, STOP_FILE_SUFFIX);
                        ret = ReadStopFile(name, g_knit_data);
                        if(ret)
                        {
                            //
                        }
                        else
                        {
                            SendYarnStopParam(g_knit_data);
                        }
                    }
                    PostMessage(hWnd, MSG_CLOSE, 0, 0);
                }
            }
            else
            {
                p->step = FILE_COPY_STEP_SEL_TO;
                SetWindowCaption(hWnd, MW_FILE_SEL2_CAPTION);
                ShowFileList(hWnd);
                InvalidateRect(hWnd, NULL, TRUE);
            }
        }
        break;
    default:
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
    }
    else if(key == SCANCODE_F1)
    {
        FILE_COPY *pwnd = (FILE_COPY *)GetWindowAdditionalData(hWnd);
        WND_FILE *p = &pwnd->wnd_file[pwnd->step];
        if(p)
        {
            p->active = 0;
            p->over_top = 0;
            if(p->file_list)
            {
                if(p->sort_file_type == SORT_FILE_BY_NAME_UP)
                {
                    p->sort_file_type = SORT_FILE_BY_NAME_DOWN;
                }
                else
                {
                    p->sort_file_type = SORT_FILE_BY_NAME_UP;
                }
                SortFileList(p->file_list, p->list_size, p->sort_file_type);
                ShowFileList(hWnd);
                SetRunFileSortType(p->sort_file_type);
            }
        }
    }
    else if(key == SCANCODE_F2)
    {
        FILE_COPY *pwnd = (FILE_COPY *)GetWindowAdditionalData(hWnd);
        WND_FILE *p = &pwnd->wnd_file[pwnd->step];
        if(p)
        {
            p->active = 0;
            p->over_top = 0;
            if(p->file_list)
            {
                if(p->sort_file_type == SORT_FILE_BY_SIZE_UP)
                {
                    p->sort_file_type = SORT_FILE_BY_SIZE_DOWN;
                }
                else
                {
                    p->sort_file_type = SORT_FILE_BY_SIZE_UP;
                }
                SortFileList(p->file_list, p->list_size, p->sort_file_type);
                ShowFileList(hWnd);
                SetRunFileSortType(p->sort_file_type);
            }
        }
    }
    else if(key == SCANCODE_F3)
    {
        FILE_COPY *pwnd = (FILE_COPY *)GetWindowAdditionalData(hWnd);
        WND_FILE *p = &pwnd->wnd_file[pwnd->step];
        if(p)
        {
            p->active = 0;
            p->over_top = 0;
            if(p->file_list)
            {
                if(p->sort_file_type == SORT_FILE_BY_TIME_DOWN)
                {
                    p->sort_file_type = SORT_FILE_BY_TIME_UP;
                }
                else
                {
                    p->sort_file_type = SORT_FILE_BY_TIME_DOWN;
                }
                SortFileList(p->file_list, p->list_size, p->sort_file_type);
                ShowFileList(hWnd);
                SetRunFileSortType(p->sort_file_type);
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        FILE_COPY *pwnd = (FILE_COPY *)GetWindowAdditionalData(hWnd);
        WND_FILE *p = &pwnd->wnd_file[pwnd->step];
        if(p)
        {
            if(p->active)
            {
                --p->active;
                SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1 + p->active));
            }
            else if(p->over_top)
            {
                --p->over_top;
                ShowFileList(hWnd);
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        FILE_COPY *pwnd = (FILE_COPY *)GetWindowAdditionalData(hWnd);
        WND_FILE *p = &pwnd->wnd_file[pwnd->step];
        if(p)
        {
            if(p->active + p->over_top + 1 < p->list_size)
            {
                if(p->active + 1 < FILE_LIST_NUM_BY_FILE_BMP)
                {
                    ++p->active;
                    SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1 + p->active));
                }
                else
                {
                    ++p->over_top;
                    ShowFileList(hWnd);
                }
            }
        }
    }
    else if(key == SCANCODE_PAGEUP)
    {
        OnPageUp(hWnd);
    }
    else if(key == SCANCODE_PAGEDOWN)
    {
        OnPageDown(hWnd);
    }
}
/*
 *
 */
static int FileCopyWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateFileCopyInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            FILE_COPY *p = (FILE_COPY *)GetWindowAdditionalData(hWnd);
            if(p)
            {
                FREE(p->wnd_file[0].file_list);
                FREE(p->wnd_file[1].file_list);
                FREE(p);
            }
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_HELP]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateFileCopyWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_HELP_WND_BMP) / 2;
    int y = (CY_SCREEN - CY_HELP_WND_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_FILE_SEL1_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = FileCopyWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_HELP_WND_BMP;
    CreateInfo.by = y + CY_HELP_WND_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = (int)calloc(1, sizeof(FILE_COPY));
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
